package com.topsec.erp.controller;

import com.topsec.erp.common.Constants;
import com.topsec.erp.entity.AdminUser;
import com.topsec.erp.service.AdminUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * author: zsl
 * date: 2019/11/22 11:13
 * Description:
 */
@Controller
public class MainController {
    // 日志记录器
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AdminUserService adminUserService;


//    退出功能
    @RequestMapping(value="/user/logout")
    public ModelAndView logout(ModelAndView mv, HttpSession session){
        session.setAttribute(Constants.USER_SESSION, null);
        session.setAttribute("tip", null);
        mv.setViewName("/loginForm");
        return mv;
    }


    @RequestMapping(value="/login")
    public String login(@RequestParam("realname") String realname,
                        @RequestParam("password") String password, Model model, HttpServletRequest request){
        HttpSession session = request.getSession();

        AdminUser user = adminUserService.login(realname, password);
        if(user != null){
            session.setAttribute(Constants.USER_SESSION, user);
            session.setMaxInactiveInterval(360000);
            model.addAttribute("","");
            return "/index";
        } else {
            System.out.println("设置登录失败提示信息");
            model.addAttribute("message", "登录名或密码错误!请重新输入");
            return "/loginForm";
        }
    }


    @RequestMapping("/")
    public String loginForm() {
        return "/loginForm";
    }

    @RequestMapping(value="/welcome")
    public String welcome(){
        String blank = "welcome";
        return blank;
    }

}